# FIXME: This, ideally, should be an exhaustive list of headers (transitively) used by libc's own
#        headers. (Like, for example, we shouldn't include AK/Platform.h in them for no reason at
#        all.)
set(HEADERS
    arch/${SERENITY_ARCH}/fenv.h
    arch/fenv.h
    arpa/inet.h
    bits/dlfcn_integration.h
    bits/FILE.h
    bits/getopt.h
    bits/mutex_locker.h
    bits/posix1_lim.h
    bits/pthread_cancel.h
    bits/pthread_integration.h
    bits/search.h
    bits/sighow.h
    bits/stdint.h
    bits/stdio_file_implementation.h
    bits/utimens.h
    bits/wchar.h
    bits/wchar_size.h
    net/if.h
    net/if_arp.h
    net/route.h
    netinet/if_ether.h
    netinet/in.h
    netinet/in_systm.h
    netinet/ip.h
    netinet/ip_icmp.h
    netinet/tcp.h
    sys/arch/${SERENITY_ARCH}/regs.h
    sys/arch/regs.h
    sys/archctl.h
    sys/auxv.h
    sys/cdefs.h
    sys/device.h
    sys/devices/gpu.h
    sys/file.h
    sys/internals.h
    sys/ioctl.h
    sys/mman.h
    sys/param.h
    sys/poll.h
    sys/prctl.h
    sys/ptrace.h
    sys/resource.h
    sys/select.h
    sys/socket.h
    sys/stat.h
    sys/statvfs.h
    sys/sysmacros.h
    sys/time.h
    sys/times.h
    sys/ttydefaults.h
    sys/types.h
    sys/uio.h
    sys/un.h
    sys/utsname.h
    sys/wait.h
    alloca.h
    assert.h
    byteswap.h
    complex.h
    ctype.h
    dirent.h
    dlfcn.h
    elf.h
    endian.h
    errno_codes.h
    errno.h
    fcntl.h
    fd_set.h
    fenv.h
    float.h
    fnmatch.h
    getopt.h
    glob.h
    grp.h
    ifaddrs.h
    inttypes.h
    langinfo.h
    libgen.h
    limits.h
    link.h
    locale.h
    mallocdefs.h
    math.h
    memory.h
    mntent.h
    netdb.h
    nl_types.h
    paths.h
    poll.h
    pthread.h
    pty.h
    pwd.h
    regex.h
    resolv.h
    sched.h
    search.h
    semaphore.h
    serenity.h
    setjmp.h
    shadow.h
    signal.h
    spawn.h
    stdarg.h
    stdint.h
    stdio_ext.h
    stdio.h
    stdlib.h
    string.h
    strings.h
    sysexits.h
    syslog.h
    termios.h
    time.h
    ucontext.h
    ulimit.h
    unistd.h
    utime.h
    utmp.h
    wchar.h
    wctype.h

    ../LibELF/ELFABI.h
    ../LibRegex/RegexDefs.h

    ../../../Kernel/API/POSIX/fcntl.h
    ../../../Kernel/API/POSIX/errno.h
    ../../../Kernel/API/POSIX/poll.h
    ../../../Kernel/API/POSIX/ifaddrs.h
    ../../../Kernel/API/POSIX/sched.h
    ../../../Kernel/API/POSIX/signal_numbers.h
    ../../../Kernel/API/POSIX/dirent.h
    ../../../Kernel/API/POSIX/stdio.h
    ../../../Kernel/API/POSIX/time.h
    ../../../Kernel/API/POSIX/select.h
    ../../../Kernel/API/POSIX/sys/wait.h
    ../../../Kernel/API/POSIX/sys/statvfs.h
    ../../../Kernel/API/POSIX/sys/limits.h
    ../../../Kernel/API/POSIX/sys/stat.h
    ../../../Kernel/API/POSIX/sys/times.h
    ../../../Kernel/API/POSIX/sys/types.h
    ../../../Kernel/API/POSIX/sys/auxv.h
    ../../../Kernel/API/POSIX/sys/resource.h
    ../../../Kernel/API/POSIX/sys/utsname.h
    ../../../Kernel/API/POSIX/sys/mman.h
    ../../../Kernel/API/POSIX/sys/time.h
    ../../../Kernel/API/POSIX/sys/uio.h
    ../../../Kernel/API/POSIX/sys/un.h
    ../../../Kernel/API/POSIX/sys/ptrace.h
    ../../../Kernel/API/POSIX/sys/socket.h
    ../../../Kernel/API/POSIX/unistd.h
    ../../../Kernel/API/POSIX/netinet/in.h
    ../../../Kernel/API/POSIX/netinet/tcp.h
    ../../../Kernel/API/POSIX/serenity.h
    ../../../Kernel/API/POSIX/futex.h
    ../../../Kernel/API/POSIX/net/if.h
    ../../../Kernel/API/POSIX/net/route.h
    ../../../Kernel/API/POSIX/net/if_arp.h
    ../../../Kernel/API/POSIX/ucontext.h
    ../../../Kernel/API/POSIX/termios.h
    ../../../Kernel/API/POSIX/signal.h

    ../../../Kernel/API/serenity_limits.h

    ../../../Kernel/Arch/${SERENITY_ARCH}/mcontext.h
    ../../../Kernel/Arch/mcontext.h
)

function(link_libc_headers libc_source_dir sysroot_include_dir)
    foreach(relative_header_path IN LISTS HEADERS)
        get_filename_component(directory ${relative_header_path} DIRECTORY)
        get_filename_component(name ${relative_header_path} NAME)
        string(REPLACE "../" "" subdirectory "${directory}")
        file(MAKE_DIRECTORY "${sysroot_include_dir}/${subdirectory}")
        file(CREATE_LINK "${libc_source_dir}/${relative_header_path}" "${sysroot_include_dir}/${subdirectory}/${name}" SYMBOLIC)
    endforeach()
endfunction()
